/*
 * Copyright (C) 2014 The Senzu Dev Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import dto.UserDTO;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import logic.User;
import util.InputHelper;

/**
 *
 * @author lucifer
 */
public class LoginPanel extends javax.swing.JPanel implements Slave {

  private Master master;

  public void SetMaster(Master master) {
    this.master = master;
  }

  public void SetModeActions() {
    // nil
  }

  private int uid;
  private int mode;

  public LoginPanel() {
    uid = -1;
    initComponents();
  }

  public LoginPanel(Master master) {
    SetMaster(master);
    initComponents();
  }

  public int UID() {
    return uid;
  }

  public int Mode() {
    return mode;
  }

  private void login() {
    UserDTO user = null;
    try {
      user = User.Search(usernamejTextField.getText(), String.valueOf(passwordjPasswordField.getPassword()));
    } catch (SQLException e) {
      JOptionPane.showMessageDialog(this, InputHelper.PanicMessage, "ERROR!", JOptionPane.ERROR_MESSAGE);
      usernamejTextField.setText("");
      passwordjPasswordField.setText("");
      System.out.printf("?0bcce4c\n");
      return;
    }

    if (user == null) {
      JOptionPane.showMessageDialog(this, "Nombre de usuario ó password incorrecto. Por favor, intente nuevamente", "Atención!", JOptionPane.WARNING_MESSAGE);
      usernamejTextField.setText("");
      passwordjPasswordField.setText("");
      return;
    }

    User u = new User(user);
    uid = u.getId();
    mode = u.getGid();

    if (u.IsRoot()) {
      ((App) master).SetRootMode();
    }

    if (u.IsRegular()) {
      ((App) master).SetRegularMode();
    }

    if (u.IsMedico()) {
      ((App) master).SetMedicoMode();
    }
  }

  public void Clean() {
    usernamejTextField.setText("");
    passwordjPasswordField.setText("");
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    passwordjPasswordField = new javax.swing.JPasswordField();
    usernamejTextField = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    loginjButton = new javax.swing.JButton();

    setMinimumSize(new java.awt.Dimension(400, 300));
    setPreferredSize(new java.awt.Dimension(890, 560));

    passwordjPasswordField.setToolTipText("Ingrese su clave.");
    passwordjPasswordField.setMaximumSize(new java.awt.Dimension(292, 27));
    passwordjPasswordField.setMinimumSize(new java.awt.Dimension(292, 27));
    passwordjPasswordField.setPreferredSize(new java.awt.Dimension(292, 27));
    passwordjPasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        passwordjPasswordFieldFocusGained(evt);
      }
    });
    passwordjPasswordField.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        passwordjPasswordFieldActionPerformed(evt);
      }
    });

    usernamejTextField.setToolTipText("Ingrese su nombre de usuario.");
    usernamejTextField.setMaximumSize(new java.awt.Dimension(292, 27));
    usernamejTextField.setMinimumSize(new java.awt.Dimension(292, 27));
    usernamejTextField.setPreferredSize(new java.awt.Dimension(292, 27));
    usernamejTextField.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        usernamejTextFieldFocusGained(evt);
      }
    });
    usernamejTextField.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        usernamejTextFieldActionPerformed(evt);
      }
    });

    jLabel1.setText("Username:");

    jLabel2.setText("Password:");

    loginjButton.setText("Login");
    loginjButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        loginjButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(262, 262, 262)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jLabel1)
          .addComponent(jLabel2))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(passwordjPasswordField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(usernamejTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(loginjButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGap(262, 262, 262))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(250, 250, 250)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(usernamejTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(passwordjPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel2))
        .addGap(18, 18, 18)
        .addComponent(loginjButton)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

  private void usernamejTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_usernamejTextFieldFocusGained
    usernamejTextField.selectAll();
  }//GEN-LAST:event_usernamejTextFieldFocusGained

  private void passwordjPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordjPasswordFieldFocusGained
    passwordjPasswordField.selectAll();
  }//GEN-LAST:event_passwordjPasswordFieldFocusGained

  private void loginjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginjButtonActionPerformed
    login();
  }//GEN-LAST:event_loginjButtonActionPerformed

 
  
  // TODO: Porque hace esto adicional??
  private void usernamejTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernamejTextFieldActionPerformed
    login();
  }//GEN-LAST:event_usernamejTextFieldActionPerformed

  private void passwordjPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordjPasswordFieldActionPerformed
    login();
  }//GEN-LAST:event_passwordjPasswordFieldActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JButton loginjButton;
  private javax.swing.JPasswordField passwordjPasswordField;
  private javax.swing.JTextField usernamejTextField;
  // End of variables declaration//GEN-END:variables
}
